'use strict';
// app/controller/news.js
const Controller = require('egg').Controller;
const qs = require('qs');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const query = ctx.query;
    const { project, env } = ctx.params;
    const baseUrl = 'http://static.yishouapp.com';
    // const time = new Date().getTime();
    const random = Math.random().toString(36).substr(2);
    if (env) {
      ctx.redirect(`${baseUrl}/app/h5/test/${project}/index.html?${qs.stringify({ testEnv: env, random })}#/?${qs.stringify(query)}`);
    } else {
      ctx.redirect(`${baseUrl}/app/h5/production/${project}/index.html?${qs.stringify({ random })}#/?${qs.stringify(query)}`);
    }
  }
  async getHtmlData() {
    const { ctx, service } = this;
    const { id, path, act_data } = ctx.request.body;
    const result = await service.jsonp.show(id);
    const data = await ctx.curl(result.api_url + path, {
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
      dataType: 'json',
      data: { id: result.act_id, ...act_data },
    });
    ctx.body = { cms_data: { ...result }, ...data.data };
  }
  async htmlUpdate() {
    const { ctx, service } = this;
    const request = ctx.request.body;
    const loginInfo = await ctx.curl(`${ctx.request.host}/api/v1/user/login`, {
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
      dataType: 'json',
      data: { captcha: 'v9am12', ...request },
    });
    // const data = await ctx.curl('http://dev16.yishou.com/api/activity/get_info', {
    //   method: 'post',
    //   headers: { 'Content-Type': 'application/json' },
    //   dataType: 'json',
    //   data: { id: result.act_id },
    // });
    ctx.body = { loginInfo };
  }
}

module.exports = HomeController;
